<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 3/19/2019
 * Time: 9:21 AM
 */

namespace p5\objects;


class JsonUtils
{

    public static function toArray($object)
    {
        if (empty($object)) {
            return [];
        }
        if (is_array($object)) {
            return $object;
        }
        if (is_string($object)) {
            if (strpos(trim($object), "[") === 0 || strpos(trim($object), "{") === 0) {
                return json_decode($object, true);
            } else {
                return explode(",", $object);
            }
        }
        return json_decode(json_encode($object), true);
    }

    public static function prettyEncode($object)
    {
        return static::encode($object, true, true);
    }

    public static function encode($object, $pretty = true, $unescapeUnicode = true)
    {
        if ($pretty) {
            if ($unescapeUnicode) {
                return json_encode($object, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
            } else {
                return json_encode($object, JSON_PRETTY_PRINT);
            }
        } else {
            if ($unescapeUnicode) {
                return json_encode($object, JSON_UNESCAPED_UNICODE);
            } else {
                return json_encode($object);
            }
        }
    }
}